package com.aaa.web.dao.dept;

import com.aaa.web.dao.dept.Impl.DeptDao;
import com.aaa.web.entity.Dept;
import com.aaa.web.util.BaseDao;
import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName Java_web_demo_20230302
 * @Author 李江波
 * @Date 2023/03/02 上午 09:53
 * @Version 1.0.0
 * @Description
 */
public class DeptDaoImpl implements DeptDao {
    @Override
    public List<Dept> queryByPage(int start, int pageSize, Dept dept) {
        //定义查询sql  where 1=1 恒成立 为拼接and条件做准备
        String sql = "select dept_no,dept_name,loc from tb_dept where 1=1 ";
        //定义一个list来动态放置查询条件
        List<String> paramList = new ArrayList<>();
        //判断部门是否为空
        if (dept!=null){
            //判断部门 名称是否为空
            if (dept.getDeptName()!=null&&!"".equals(dept.getDeptName())){
                sql+="and dept_name like ?";
                paramList.add("%"+dept.getDeptName()+"%");
            }
            if (dept.getLoc()!=null&&!"".equals(dept.getLoc())){
                sql+="and loc like ?";
                paramList.add("%"+dept.getLoc()+"%");
            }
        }
        sql+=" limit " + start +","+pageSize;
        System.out.println("execute sql -------- " + sql);
        //paramList.toArray()集合转数组
        return BaseDao.executeQueryA(sql,Dept.class,paramList.toArray());
    }

    @Override
    public int queryTotal(Dept dept) {
        //定义查询sql  where 1=1 恒成立 为拼接and条件做准备
        String sql = "select *  from tb_dept where 1=1 ";
        //定义一个list来动态放置查询条件
        List<String> paramList = new ArrayList<>();
        //判断部门是否为空
        if (dept!=null){
            //判断部门 名称是否为空
            if (dept.getDeptName()!=null&&!"".equals(dept.getDeptName())){
                sql+="and dept_name like ?";
                paramList.add("%"+dept.getDeptName()+"%");
            }
            if (dept.getLoc()!=null&&!"".equals(dept.getLoc())){
                sql+="and loc like ?";
                paramList.add("%"+dept.getLoc()+"%");
            }
        }
        System.out.println("execute sql  -------- :" + sql);
        List<Dept> mapList = BaseDao.executeQueryA(sql,Dept.class,paramList.toArray());
        System.out.println(mapList.size());
        return mapList.size();
    }

    @Override
    public List<Dept> queryAll() {
        return BaseDao.executeQueryA("select dept_no,dept_name,loc from tb_dept",Dept.class);
    }

    @Override
    public List<Dept> queryById(int deptNo) {
        return BaseDao.executeQueryA("select dept_no,dept_name,loc from tb_dept where dept_no=?",Dept.class,deptNo);
    }

    @Override
    public int add(Dept dept) {
        return BaseDao.executeUpdate("insert into tb_dept(dept_name,loc) values(?,?)",
                new Object[]{dept.getDeptName(),dept.getLoc()});
    }

    @Override
    public int update(Dept dept) {
        return BaseDao.executeUpdate("update tb_dept set dept_name=?,loc=? where dept_no=?",
                new Object[]{dept.getDeptName(),dept.getLoc(),dept.getDeptNo()});
    }

    @Override
    public int deleteById(int deptNo) {
        return BaseDao.executeUpdate("delete from tb_dept where dept_no=?",
                new Object[]{deptNo});
    }
}
